package com.cupdata.zicon.my.proxy;

import java.lang.reflect.Method;

/**
 * 自定义aop的通用实现
 */
public class DefaultAOPProxyImpl implements InvocationHandler{
    private Object object;
    private AOPMethod aopMethod;
    public DefaultAOPProxyImpl(Object object, AOPMethod aopMethod){
        this.aopMethod=aopMethod;
        this.object=object;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
        try {
            aopMethod.before();
            Object invoke = method.invoke(object, args);
            aopMethod.after();
            return invoke;
        }catch (Exception e){
            return aopMethod.onException(e);
        }
    }

    @Override
    public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }

    public AOPMethod getAopMethod() {
        return aopMethod;
    }

    public void setAopMethod(AOPMethod aopMethod) {
        this.aopMethod = aopMethod;
    }
}
